package com.tjsinfo.tjsoa.vote.action;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.vote.bo.ITVoteConformationBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteEstimatedStatusBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteValueBO;
import com.tjsinfo.tjsoa.vote.vo.TVoteConformation;
import com.tjsinfo.tjsoa.vote.vo.TVoteEstimatedStatus;
import com.tjsinfo.tjsoa.vote.vo.TVoteValue;

public class ValueAddAction extends Action {
	private ITVoteValueBO valueBO;
	private ITVoteEstimatedStatusBO estimatedStatusBo;
	private ITVoteConformationBO conformationBo;

	public void setConformationBo(ITVoteConformationBO conformationBo) {
		this.conformationBo = conformationBo;
	}

	public void setEstimatedStatusBo(ITVoteEstimatedStatusBO estimatedStatusBo) {
		this.estimatedStatusBo = estimatedStatusBo;
	}

	public void setValueBO(ITVoteValueBO valueBO) {
		this.valueBO = valueBO;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws SQLException {
		try {
			
			String headId = request.getParameter("headId");
			String estimatedStatusId= request.getParameter("estimatedStatusId");
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String message = "vote.valueAdd.message";
			
			TVoteEstimatedStatus ves = estimatedStatusBo.findById(estimatedStatusId);
			List<TVoteConformation> conformationLists = conformationBo.find(headId);
			for(TVoteConformation conformationList:conformationLists){
				TVoteValue vv = new TVoteValue();
				vv.setHeadId(headId);
				vv.setVote(userId.toString());
				vv.setEstimated(ves.getEstimatedId());
				vv.setEstimatedName(ves.getName());
				vv.setConformationId(conformationList.getId());
				if ("0".equals(request.getParameter("valueSaveId"))) {
					vv.setValueKey(request.getParameter("valueKey"+conformationList.getId()));
				} else {
					vv.setValueKey("0");
					message = "vote.valueAdd1.message";
				}
				valueBO.save(vv);
			}
			ves.setStatus(1);
			estimatedStatusBo.update(ves);
			
			
			request.setAttribute("linkurl", "tjsinfo/tjsoa/vote/estimatedStatusList.do?headId="+headId+"");
			request.setAttribute("message", message);				
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}

}
